public class ThreadDemo2 {
    public static void main(String[] args) {

    }
    public static void main5(String[] args) {
        Thread thread=Thread.currentThread();//获取当前线程
        System.out.println(thread.getName());
    }
    public static void main4(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("等我，拿一个书包");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t1.start();
        t1.join(10000);//等待t1线程结束，t1线程不结束，main线程就进入阻塞状态
        System.out.println("等你来，我才走");
    }

    public static void main3(String[] args) throws InterruptedException {
        System.out.println("原本的中断标志位为：" + Thread.interrupted());
        //这里打印出来Thread.interrupted()是false;
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                System.out.println("现在的中断标志位为" + Thread.currentThread().isInterrupted());
            }
        });
        t1.start();
        t1.interrupt();//这里是将中断标志位设置为true;
    }

    public static void main2(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("她不在，可以继续布置");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("她来了，快撤");
        });
        t1.start();
        Thread.sleep(1);
        t1.interrupt();//这个就是将t1这个线程中断
    }

    private static boolean flag = false;

    public static void main1(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (!flag) {
                System.out.println("没被发现，可以继续潜伏");
            }
            System.out.println("快撤！！");
        });
        t1.start();
        Thread.sleep(1);
        flag = true;
    }
}
